#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "httpd_post_values.h"
#include "lfs_rp2040.h"

static lfs_file_t *g_http_post_file;
void httpd_post_value_init(char *uri)
{
    g_http_post_file = NULL;
}

void httpd_post_value_set(char *uri, char *name, char *data, int len)
{
    if ((name == NULL) || (name[0] == '\0')) {
        name = "temp.bin";
    }
    if (g_http_post_file == NULL) {
        g_http_post_file = lfs_spiflash_open(name, LFS_O_WRONLY | LFS_O_CREAT, false);
    }
    if (g_http_post_file) {
        lfs_spiflash_write(g_http_post_file, data, len);
    }
}

void httpd_post_value_finished(char *response_uri, uint16_t response_uri_len)
{
    if (g_http_post_file != NULL) {
        lfs_spiflash_close(g_http_post_file);
        g_http_post_file = NULL;
    }
    strcpy(response_uri, "/succ.html"); /* 拷贝uri 用于给浏览器响应相应的请求 */   
}